package com.huika.renrench.control.investment.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.investment.adapter.InvestmentRecordAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：投资记录
 * @author zc
 * @date 2015年6月2日 上午10:00:58
 */
public class InvestmentRecordActivity extends BaseAct implements Listener<RequestResult<ArrayList<HashMap<String, String>>>>, OnRefreshListener2<ListView> {

	private TextView name;
	private TextView money;
	private TextView time;
	private PullToRefreshListView record_list;
	private boolean isUpRefresh;
	private int pageNo = 1;
	private int pageSize = 15;
	private String userId;
	private InvestmentRecordAdapter adapter;
	private ArrayList<HashMap<String, String>> mapList = new ArrayList<HashMap<String,String>>();
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_investment_record);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		name = (TextView) findViewById(R.id.name);
		money = (TextView) findViewById(R.id.money);
		time = (TextView) findViewById(R.id.time);
		record_list = (PullToRefreshListView) findViewById(R.id.record_list);
		
		adapter = new InvestmentRecordAdapter(this);
		adapter.setGroup(mapList);
		record_list.setAdapter(adapter);
		record_list.setOnRefreshListener(this);
		getInvestInfoList(userId, pageNo, pageSize);
	} 
	
	@Override
	protected void initData() {
		super.initData();
		userId = GlobalApp.getInstance().getUserId();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("投资");
		barHelper.setTitleMsg("投资记录");
	}
	
	private void getInvestInfoList(String userId, int pageNo, int pageSize) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("pageNo", pageNo);
		ajaxParams.putCommonTypeParam("pageSize", pageSize);
		FormResultRequest<ArrayList<HashMap<String, String>>> request = new FormResultRequest<ArrayList<HashMap<String, String>>>(UrlConstants.GETINVESTINFOLIST, this, this, new TypeToken<RequestResult<ArrayList<HashMap<String, String>>>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	@Override
	public void onResponse(RequestResult<ArrayList<HashMap<String, String>>> arg0) {
		record_list.onRefreshComplete();
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			if(arg0.getRs() != null && arg0.getRs().size() == pageSize) {
				record_list.setMode(Mode.BOTH);
			} else {
				record_list.setMode(Mode.PULL_FROM_START);
			}
			
			if(isUpRefresh) {
				mapList.addAll(arg0.getRs());
				adapter.setGroup(mapList);
			} else {
				mapList = arg0.getRs();
				adapter.setGroup(mapList);
			}
			isUpRefresh = false;
		}
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		record_list.onRefreshComplete();
		if(isUpRefresh) {
			pageNo--;
		}
		isUpRefresh = false;
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		pageNo = 1;
		getInvestInfoList(userId, pageNo, pageSize);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		isUpRefresh = true;
		getInvestInfoList(userId, ++pageNo, pageSize);
	}

}
